package com.lee.day02;

import com.lee.model.OrderModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @Description 函数式声明binding.
 * @Author winston
 * @DateTime 2022/8/26
 */
@Configuration
public class NewMessageBinding {

    /**
     * 生产者
     * 不停地生产
     *
     * @return {@link Supplier}<{@link OrderModel}>
     */
//    @Bean
    public Supplier<OrderModel> newOrderProducer () {
        return () -> {
            OrderModel orderModel = new OrderModel();
            orderModel.setOrderNo("new order no");
            orderModel.setOrderName("new order name");
            orderModel.setId(111222L);
            System.out.println("生产消息: "+ orderModel);
            return orderModel;
        };
    }


    /**
     * 先消费, 再生产, 作为中间处理环节使用, 适用于中间加工一下场景.
     *
     * @return {@link Function}<{@link OrderModel}, {@link String}>
     */
    @Bean
    public Function<OrderModel, String> convert() {
        return (order)->{
            System.out.println("Function consumer message: "+order.toString());
            return order +": after function deal.";
        };
    }

    /**
     * 消费者
     *
     * @return {@link Consumer}<{@link String}>
     */
    @Bean
    public Consumer<String> completeOrder() {
        return  (order)->{
            System.out.println("最新消费到的消息: "+ order);
        };
    }
}
